<template>
  <div class="box">
    <el-form :model="queryParams" ref="queryForm" class="input" size="small" :inline="true" v-show="showSearch"
      label-width="68px">
      <el-form-item prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择可售状态">
          <el-option v-for="item in stateList" :key="item.id" :label="item.name" :value="item.id + ''">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryParams.name" prefix-icon="el-icon-search" placeholder="请输入会员卡名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btn" size="mini" @click="handleQuery" v-hasPermi="['pet:card:query']">查询</el-button>
        <el-button type="primary" class="btn" size="mini" v-hasPermi="['pet:card:add']" @click="handleAdd">新增会员卡模板</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="cardList">
      <el-table-column label="卡类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ scope.row.type == 1 ? '门店使用' : scope.row.type == 2 ? '小程序使用' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="会员卡名称" align="center" prop="name" />
      <el-table-column label="是否参与积分" align="center" prop="isForPoint">
        <template slot-scope="scope">
          {{ scope.row.isForPoint == 1 ? '是' : scope.row.isForPoint == 2 ? '否' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="开卡用户数" align="center" prop="" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pet:card:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pet:card:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改会员卡信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" class="center" append-to-body>
      <el-form ref="form" :model="form" class="input" :rules="rules" label-width="100px">
        <el-form-item label="卡类型" prop="name">
          <el-select v-model="form.type" style="width:100%" placeholder="请选择卡类型">
            <el-option label="门店使用" value="1" />
            <el-option label="小程序使用" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="会员卡名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入会员卡名称" />
        </el-form-item>
        <el-form-item label="是否参与积分" prop="isForPoint">
          <el-radio v-model="form.isForPoint" label="1">是</el-radio>
          <el-radio v-model="form.isForPoint" label="2">否</el-radio>
        </el-form-item>
        <el-form-item label="开卡用户数" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入开卡用户数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" style="padding: 12px 70px;" class="btn" @click="submitForm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCard, getCard, delCard, addCard, updateCard } from "@/api/pet/card";

export default {
  name: "Card",
  data() {
    return {
      stateList: [
        {
          id: 1,
          name: '(门店)可售'
        },
        {
          id: 2,
          name: '(小程序)可售'
        },
        {
          id: 3,
          name: '(不限渠道)可售'
        },
        {
          id: 4,
          name: '禁售'
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员卡信息表格数据
      cardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: null,
        appName: null,
        appRemark: null,
        isForPoint: null,
        discount: null,
        url: null,
        updateUser: null,
        createUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员卡信息列表 */
    getList() {
      this.loading = true;
      listCard(this.queryParams).then(response => {
        this.cardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        name: null,
        appName: null,
        appRemark: null,
        isForPoint: null,
        discount: null,
        remark: null,
        url: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员卡信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCard(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员卡信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员卡信息编号为"' + ids + '"的数据项？').then(function () {
        return delCard(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/card/export', {
        ...this.queryParams
      }, `card_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
